/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.base.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


import com.tekj.cxqcb.R;

import java.util.Timer;
import java.util.TimerTask;

import common.base.enums.PubDialogLoadingEnum;

/**
 * 公告的dialogloading载入页面调用方法
 * Created by JustTong on 2018/6/29.
 * //        mPubDialogLoading = new PubDialogLoading(this);//必须在Presenter前初始化
 * //        mPubDialogLoading = new PubDialogLoading(this,PubDialogLoadingEnum.全屏显示);//必须在Presenter前初始化
 * //        mPubDialogLoading = new PubDialogLoading(this,PubDialogLoadingEnum.覆盖显示,true);//必须在Presenter前初始化
 * //        mPubDialogLoading = new PubDialogLoading(mActivity, false);//必须在Presenter前初始化
 */
public class PubDialogLoading {

    private Dialog mLoadingDialog;
    private Activity mActivity;
    private boolean mIsCancelable = false;//默认-不可取消
    private PubDialogLoadingEnum mEnumPubDialogLoading = PubDialogLoadingEnum.全屏显示;//默认-全屏显示
    private boolean mIsDelayed;//是否延时关闭

    /**
     * 默认为不可取消的--覆盖显示dialog
     *
     * @param activity
     */
    public PubDialogLoading(Activity activity) {
        this.mActivity = activity;

        //业务执行
        PubDialogLoadingDo();
    }

    /**
     * 默认为不可取消的dialog
     *
     * @param activity
     * @param enumPubDialogLoading
     */
    public PubDialogLoading(Activity activity, PubDialogLoadingEnum enumPubDialogLoading) {
        this.mActivity = activity;
        this.mEnumPubDialogLoading = enumPubDialogLoading;

        //业务执行
        PubDialogLoadingDo();
    }


    /**
     * 不可取消的--覆盖显示dialog,是否支持延时关闭
     *
     * @param activity
     * @param isDelayed
     */
    public PubDialogLoading(Activity activity, boolean isDelayed) {
        this.mActivity = activity;
        this.mEnumPubDialogLoading = PubDialogLoadingEnum.覆盖显示;
        this.mIsDelayed = isDelayed;

        //业务执行
        PubDialogLoadingDo();
    }

    /**
     * 公告的dialogloading载入页面调用方法-方法封装
     *
     * @param activity             关联activity
     * @param enumPubDialogLoading 显示模式
     * @param isCancelable         是否可取消
     */
    public PubDialogLoading(Activity activity, PubDialogLoadingEnum enumPubDialogLoading, boolean isCancelable) {
        this.mActivity = activity;
        this.mIsCancelable = isCancelable;
        this.mEnumPubDialogLoading = enumPubDialogLoading;

        //业务执行
        PubDialogLoadingDo();

    }

    /**
     * 业务执行
     */
    private void PubDialogLoadingDo() {

        //初始化mLoadingDialog
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View dialog;

        switch (mEnumPubDialogLoading) {
            case 全屏显示:
                dialog = inflater.inflate(R.layout.loading_full, null);
                mLoadingDialog = new Dialog(mActivity, R.style.AppTheme);
                mLoadingDialog.setContentView(dialog);
                mLoadingDialog.setCancelable(mIsCancelable);

                mLoadingDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                mLoadingDialog.getWindow().setWindowAnimations(R.style.DialogAnimation); // 添加动画
                mLoadingDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                break;
            case 覆盖显示:
                dialog = inflater.inflate(R.layout.loading_spinkit, null);
                mLoadingDialog = new Dialog(mActivity, R.style.CustomDialog);
                mLoadingDialog.setContentView(dialog);
                mLoadingDialog.setCancelable(mIsCancelable);
                break;
        }
    }


    public void show() {
        if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void dismiss() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
//            mLoadingDialog.dismiss();

            if (mIsDelayed) {
                /**
                 * 延迟关闭
                 */
                Timer timer = new Timer();
                TimerTask tast = new TimerTask() {
                    @Override
                    public void run() {
                        mLoadingDialog.dismiss();
//                    mActivity.runOnUiThread(new Runnable() {
//                        public void run() {
//                            mLoadingDialog.dismiss();
//                        }
//                    });
                    }
                };
                timer.schedule(tast, 500);
            } else {
                mLoadingDialog.dismiss();
            }

        }
    }

}
